-module(test).

-export([start/0, foo/1, t/1]).

start() ->
	Pid = spawn(fun() ->loop() end),
	Pid3 = spawn(fun() ->loop_monitor(Pid) end),
	io:format("Pid ~p~nPid3 ~p~n", [Pid,Pid3]).

loop_monitor(Pid) ->
    process_flag(trap_exit, true),
	_MonitorRef = erlang:monitor(process, Pid),
	receive
		Msg ->
			io:format("pid exit: ~p~n", [Msg])
	end.

loop() ->
	receive
		Msg ->
			io:format("pid3 exit: ~p~n", [Msg])
	end.

foo(1) ->
    hello;
foo(2) ->
    throw({myerror, abc});
foo(3) ->
    tuple_to_list(a);
foo(4) ->
    exit({myExit, 222}).

t(X) ->
    case catch foo(X) of
      {myerror, Args} ->
           {user_error, Args};
      {'EXIT', What} ->
           {caught_error, What};
      Other ->
           Other
    end.
